﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using edu.Model;
using Discuz.Data;
using System.Data.Common;
using System.Data;
using System.Data.SqlClient;

namespace edu.Service
{
    public class ClassService
    {
        public static int Add(Class entity)
        {
            int id = 0;
            string sql = "insert into edu_class([Code],[Name],[Description],[SchoolId],[Grade]) values(@Code,@Name,@Description,@SchoolId,@Grade)";
            DbParameter[] param ={
                new SqlParameter(){DbType=DbType.String, ParameterName="Code", Value=entity.Code},
                new SqlParameter(){DbType=DbType.String, ParameterName="Name", Value=entity.Name},
                new SqlParameter(){DbType=DbType.String, ParameterName="Description", Value=entity.Description},
                new SqlParameter(){DbType=DbType.String, ParameterName="SchoolId", Value=entity.SchoolId},
                new SqlParameter(){DbType=DbType.String, ParameterName="Grade", Value=entity.Grade}
                                };
            DbHelper.ExecuteNonQuery(out id, CommandType.Text, sql, param);
            return id;
        }

        public static void Update(Class entity)
        {
            string sql = string.Format("update edu_class set [Code]=@Code,[Name]=@Name,[Description]=@Description,[SchoolId]=@SchoolId,[Grade]=@Grade where Id=@Id");
            DbParameter[] param ={
                new SqlParameter(){DbType=DbType.String, ParameterName="Code", Value=entity.Code},
                new SqlParameter(){DbType=DbType.String, ParameterName="Name", Value=entity.Name},
                new SqlParameter(){DbType=DbType.String, ParameterName="Description", Value=entity.Description},
                new SqlParameter(){DbType=DbType.String, ParameterName="SchoolId", Value=entity.SchoolId},
                new SqlParameter(){DbType=DbType.String, ParameterName="Grade", Value=entity.Grade},
                new SqlParameter(){DbType=DbType.Int32, ParameterName="Id", Value=entity.Id}
                                };
            DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }

        public static void Delete(Class entity)
        {

        }

        public static void Delete(int id)
        {
            string sql = string.Format("delete from edu_class where Id=@Id");
            DbParameter[] param ={
                new SqlParameter(){DbType=DbType.String, ParameterName="Id", Value=id}
                              };
            DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }

        public static DataTable GetClassBySchoolId(int schoolId)
        {
            string sql = string.Format("select [Id],[Code],[Name] from edu_class where [SchoolId]=@SchoolId");
            DbParameter[] parm ={
                new SqlParameter(){DbType=DbType.Int32,ParameterName="SchoolId",Value=schoolId}
            };
            return DbHelper.ExecuteDataset(CommandType.Text, sql, parm).Tables[0];
        }


        public static DataTable GetClassByTeacherId(int teacherId)
        {
            string sql = @"select a.[Id],a.[Code],a.[Name],a.[Grade],a.[ClassNum]
                        from edu_class a left join edu_teacherClass b on a.id= b.classId
                        where b.[teacherId] = @TeacherId";
            DbParameter[] parm ={
                new SqlParameter(){DbType=DbType.Int32,ParameterName="TeacherId",Value=teacherId}
            };
            return DbHelper.ExecuteDataset(CommandType.Text, sql, parm).Tables[0];
        }
    }
}
